package cn.jdy.authority.admin;

import cn.jdy.authority.entity.Organ;
import cn.jdy.authority.entity.User;
import cn.jdy.authority.service.OrganService;
import cn.jdy.framework.constant.BaseConstant;
import cn.jdy.framework.exception.BaseException;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.StringUtil;

import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

/**
 * 组织机构业务控制类
 *
 * @description:组织机构业务控制类
 * @author: Wuzhiliang 2013-08-05 16:53
 */
@Controller
@RequestMapping("/organ")
public class OrganCtrl {

	// 自动注入organ组织机构业务操作接口
	@Autowired
	private OrganService organ;
	@Autowired
	protected HttpSession session;
	/**
	 * 初始组织机构添加页面
	 *
	 * @return ModelAndView /s9/organ/organ_init.jsp
	 * @description: 跳转至角色管理页面
	 * @param: void
	 * @author: wuzhiliang
	 * @date: 2013-08-05 16:53
	 */
	@RequestMapping("/forInit")
	public ModelAndView forInit() {
		return new ModelAndView("/platform/organ.jsp");
	}

	/**
	 * 查找组织机构信息列表
	 *
	 * @description:查找组织机构信息列表 codeSortId 组织机构ID curNo 分页-页码 curSize 分页-页面加载记录条数
	 * @author: WuZhiliang
	 * @date: 2013-08-05 16:53
	 * @return: map
	 */
	@RequestMapping("/findOrganList")
	@ResponseBody
	public Map<String, Object> findOrganList(Organ bo) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
	    /* 查询条件 */
		//	int allSize = 0;2
		//	jsonMap.put("Rows", organ.findAll());
			//jsonMap.put("Total", allSize);
			/*设置最高权限k*/
			User oper = (User) session.getAttribute("user");
		   if(!oper.getUsername().equals("superadmin")){
			   bo.setPid("999999");
		   }
		   /*设置最高权限s*/
			jsonMap.put("Rows", organ.query(bo));
			jsonMap.put("Total", organ.count(bo));
		} catch(BaseException e) {
			BaseLog.e(this.getClass(), "查找组织机构信息列表出错! findOrganList! ", e);
		}
		return jsonMap;
	}

	/**
	 * 初始组织机构编辑页面
	 *
	 * @return ModelAndView /s9/organ/organ_edit.jsp
	 * @description: 初始组织机构编辑页面
	 * @param: id
	 * @author: wuzhiliang
	 * @date: 2013-08-05 16:53
	 */
	@RequestMapping("/forEditOrgan")
	@ResponseBody
	public Organ forEditOrgan(String id) {
		Organ org = null;
		try {
			if(!StringUtil.isNullOrEmpty(id)) {
				org = organ.findOne("id", id);
			}
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "获取组织机构信息出错! forEditOrgan! ", e);
		}
		return org;
	}

	/**
	 * 保存或修改组织机构信息
	 *
	 * @return map
	 * @description: 保存或修改组织机构信息 机构ID 不为空 则修改
	 * @param: void
	 * @author: wuzhiliang
	 * @date: 2013-08-05 16:53
	 */
	@RequestMapping("/forSaveOrUpdOrgan")
	@ResponseBody
	public String forSaveOrgan(Organ bo) {
		String status = "200";
		try {
			if(null != bo) {
				if(!(bo.getId() == null || "".equals(bo.getId()))) { // 机构ID不为空 则修改
					organ.update(bo);
				} else {
					organ.save(bo);
				}
			}
		} catch(BaseException e) {
			status = "500";
			BaseLog.e(this.getClass(), "保存或修改组织机构信息出错! forSaveOrgan! ", e);
		}
		return status;
	}

	/**
	 * 修改组织机构状态
	 *
	 * @return map
	 * @description: 修改组织机构状态, 返回修改组织机构状态是否成功信息
	 * @param: organIds {组织机构ID,组织机构ID ……}
	 * @author: wuzhiliang
	 * @date: 2013-08-05 16:53
	 */
	@RequestMapping("/updOrgStatus")
	@ResponseBody
	public Map<String, Object> updOrgStatus(String isactive, String organIds) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			if(!StringUtil.isNullOrEmpty(organIds)) {
				organ.updOrgStatus(isactive, organIds);
				jsonMap.put("msg", "success");
			}
		} catch(BaseException e) {
			jsonMap.put("msg", "error");
			BaseLog.e(this.getClass(), "修改组织机构状态出错! delOrgan! ", e);
		}
		return jsonMap;
	}

	/**
	 * 删除组织机构信息
	 *
	 * @return map
	 * @description: 删除组织机构信息, 返回删除是否成功信息
	 * @param: organIds {组织机构ID,组织机构ID ……}
	 * @author: wuzhiliang
	 * @date: 2013-08-05 16:53
	 */
	@RequestMapping("/delOrgan")
	@ResponseBody
	public Map<String, Object> delOrgan(String organIds) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			if(!StringUtil.isNullOrEmpty(organIds)) {
				organ.delOrgan(organIds);
				jsonMap.put("msg", "success");
			}
		} catch(BaseException e) {
			jsonMap.put("msg", "error");
			BaseLog.e(this.getClass(), "删除组织机构信息出错! delOrgan! ", e);
		}
		return jsonMap;
	}

	/**
	 * 校验机构编码是否存在
	 *
	 * @return map
	 * @description: 校验机构编码是否存在
	 * @param: organNo 机构编码
	 * @author: wuzhiliang
	 * @date: 2013-08-05 16:53
	 */
	@RequestMapping("/findOrgCount")
	@ResponseBody
	public Map<String, Object> findOrgCount(String organNo) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			if(!StringUtil.isNullOrEmpty(organNo)) {
				Organ org = new Organ();
				int count = organ.findOrganCount(org);

				jsonMap.put("count", count);

				jsonMap.put("msg", "success");
			}
		} catch(BaseException e) {
			jsonMap.put("msg", "error");
			BaseLog.e(this.getClass(), "校验机构编码是否存在出错! findOrgCount! ", e);
		}
		return jsonMap;
	}

	/**
	 * 获取组织机构树
	 *
	 * @return map
	 * @description: 获取组织机构树
	 * @param: organNo 机构编码
	 * @author: wuzhiliang
	 * @date: 2013-08-05 16:53
	 */
	@RequestMapping("/organTree")
	@ResponseBody
	public Map<String, Object> organTree(String istype) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			/*设置最高权限k*/
			User oper = (User) session.getAttribute("user");
			DetachedCriteria dc = DetachedCriteria.forClass(Organ.class);
		   	Organ bo=new Organ();
		    if(istype!=null&&istype.equals("ff8080814db89f0a014db8d169290000")){
		    	jsonMap.put("orgList", organ.findAll());
				   
			   }else if(istype!=null&&istype.equals("bd0800415b28af54015b28b63d770000")){//直播管理员
				    bo.setPid("999999");
				    dc.add(Restrictions.like("pid", bo.getPid(),MatchMode.ANYWHERE));
					jsonMap.put("orgList", organ.find(dc));
			   }else{
					jsonMap.put("orgList", organ.findAll());
			   }

	
			jsonMap.put("msg", "success");
		} catch(BaseException e) {
			jsonMap.put("msg", "error");
			BaseLog.e(this.getClass(), "获取组织机构树出错! findOrgCount! ", e);
		}
		return jsonMap;
	}

}
